package com.sgb.kjwl.viewmodel.self.publish;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.model.entity.user.PackersStateEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.fragment.self.PackersStateFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.List;

import io.reactivex.functions.Consumer;

public class PackersStateModel extends BaseViewModel {
    private PackersStateFragment fragment;
    private SelfRepertory repertory;
    private int page = 1;
    private int size = 10;
    public int total;

    public PackersStateModel(PackersStateFragment fragment, SelfRepertory repertory) {
        this.fragment = fragment;
        this.repertory = repertory;
    }

    public void getPackersStateModel(String state) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        page = 1;
        repertory.getPublishPackers(state, page, size)
                .subscribe(new Consumer<BaseEntity<ListResponse<List<PackersStateEntity>>>>() {
                    @Override
                    public void accept(BaseEntity<ListResponse<List<PackersStateEntity>>> listResponseBaseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        total = listResponseBaseEntity.getData().getTotal();
                        if (total > 0) {
                            fragment.refresh(listResponseBaseEntity.getData().getList());
                        } else {
                            fragment.empty();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                        fragment.empty();
                    }
                });
    }

    public void getPackersStateMoreModel(String state) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        page = page + 1;
        repertory.getPublishPackers(state, page, size)
                .subscribe(new Consumer<BaseEntity<ListResponse<List<PackersStateEntity>>>>() {
                    @Override
                    public void accept(BaseEntity<ListResponse<List<PackersStateEntity>>> listResponseBaseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        total = listResponseBaseEntity.getData().getTotal();
                        fragment.loadMore(listResponseBaseEntity.getData().getList());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                        fragment.loadMoreComplete();
                    }
                });
    }

    public void delModel(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.delPackersList(id)
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        fragment.delSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void onPackerModel(String id, String state) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.onPacker(id)
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        if (baseEntity.getState().equals(AppConstant.REQEUST_SECCUESS)) {
                            fragment.onlineOffSuccess(state);
                        } else {
                            MToast.showToast(fragment.getActivity(), baseEntity.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }

    public void offPackerModel(String id, String state) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        repertory.offPacker(id)
                .subscribe(new Consumer<BaseEntity>() {
                    @Override
                    public void accept(BaseEntity baseEntity) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        fragment.onlineOffSuccess(state);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        DialogHelper.dismissProgressDialog();
                        MLog.d(AppConstant.API_LOG, "error " + throwable.getMessage());
                    }
                });
    }
}
